package com.pym.bs.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pym.bs.dao.DemosMapper;
import com.pym.bs.domain.Demos;
import com.pym.bs.dto.DemosDTO;
import com.pym.bs.enums.ResultEnum;
import com.pym.bs.service.DemosService;
import com.pym.bs.utils.PageVOUtils;
import com.pym.bs.utils.Pageable;
import com.pym.bs.utils.ResultVOUtils;
import com.pym.bs.vo.PageVO;
import com.pym.bs.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DemosServiceImpl implements DemosService {

    @Resource
    private DemosMapper demosMapper;

    @Override
    public PageVO<DemosDTO> list(DemosDTO demosDTO, Pageable pageable) {
        PageHelper.startPage(pageable);
        if (demosDTO.getSelect() != "") {
            demosDTO.setWords(demosDTO.getSelect());
        }

        Page<DemosDTO> list = demosMapper.selectByKeyWords(demosDTO);
        return PageVOUtils.of(list.getResult(), list.getTotal());
    }

    @Override
    public ResultVO<DemosDTO> del(Integer id) {
        try {
            demosMapper.deleteByPrimaryKey(id);
            return ResultVOUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtils.error(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @Override
    public ResultVO<DemosDTO> edit(DemosDTO demosDTO) {
        Demos demos = new Demos();
        BeanUtils.copyProperties(demosDTO, demos);
//        demosMapper.updateByPrimaryKeySelective(demos);
        return ResultVOUtils.success();
    }
}
